﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Design;
using System.ComponentModel;
using System.Windows.Forms.Design;
using BaseTypesLib.Model.Backgrounds;
using System.Diagnostics;
using System.Drawing.Drawing2D;
using System.Drawing;

namespace BaseTypesLib.UiTypeEditors
{
    public class BackgroundEditor:UITypeEditor
    {
        BackgroundControl ctrl;
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.DropDown;
        }
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            IWindowsFormsEditorService service = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
            if(service==null)
            {
                return base.EditValue(context, provider, value);
            }

            if (ctrl == null)
                ctrl = new BackgroundControl((Background)value);
            
            service.DropDownControl(ctrl);
            Background bg = ctrl.GetBackground();
            ctrl = null;
            return bg;
        }
        public override bool GetPaintValueSupported(ITypeDescriptorContext context)
        {
            return true;
        }
        public override void PaintValue(PaintValueEventArgs e)
        {
            object bg = e.Value;

            if (bg is TransparentColorBackground)
            {
                TransparentColorBackground tbg = (TransparentColorBackground)bg;
                System.Drawing.Color col = System.Drawing.Color.FromArgb(tbg.Alpha, tbg.Color.R, tbg.Color.G, tbg.Color.B);
                System.Drawing.SolidBrush br = new System.Drawing.SolidBrush(col);
                e.Graphics.FillRectangle(br, e.Bounds);
            }
            else if (bg is SolidColorBackground)
            {
                SolidColorBackground sbg = (SolidColorBackground)bg;
                System.Drawing.Color col = System.Drawing.Color.FromArgb(sbg.Color.R, sbg.Color.G, sbg.Color.B);
                System.Drawing.SolidBrush br = new System.Drawing.SolidBrush(col);
                e.Graphics.FillRectangle(br, e.Bounds);
            }
            else if (bg is LinearGradientBackground)
            {
                LinearGradientBackground gbg = (LinearGradientBackground)bg;
                LinearGradientBrush br = null;
                if (gbg.Orientation == GradientOrientation.Horizontal)
                    br = GetGradientBrush(true, gbg.Color1, gbg.Color2, e.Bounds.Width, e.Bounds.Height);
                else
                    br = GetGradientBrush(false, gbg.Color1, gbg.Color2, e.Bounds.Width, e.Bounds.Height);

                e.Graphics.FillRectangle(br, e.Bounds);
            }
            else
            {//image
            }
        }
        private LinearGradientBrush GetGradientBrush(bool horiz, System.Windows.Media.Color c1, System.Windows.Media.Color c2, int w, int h)
        {
            Point p1 = new Point();
            Point p2 = new Point(w, h);
            if (horiz)
                p2 = new Point(w, 0);
            else
                p2 = new Point(0, h);

            Color dc1 = Color.FromArgb((byte)c1.R, (byte)c1.G, (byte)c1.B);
            Color dc2 = Color.FromArgb((byte)c2.R, (byte)c2.G, (byte)c2.B);
            LinearGradientBrush brush = new LinearGradientBrush(p1, p2, dc1, dc2);
            return brush;
        }
    }
}
